package concreteClass;

import abstractClass.AbstractDaoTemplate;
import entity.User;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 用户数据访问对象 - 具体子类
 */
public class UserDao extends AbstractDaoTemplate<User> {
    
    @Override
    protected Connection getConnection() throws SQLException {
        // 返回数据库连接
        //return DataSourceUtil.getConnection();
        return null;
    }
    
    @Override
    protected User rowMapper(ResultSet rs) throws SQLException {
        User user = new User();
        user.setId(rs.getLong("id"));
        user.setUsername(rs.getString("username"));
        user.setEmail(rs.getString("email"));
        user.setCreateTime(rs.getTimestamp("create_time"));
        return user;
    }
    
    // 具体的业务方法
    public User findById(Long id) {
        String sql = "SELECT * FROM users WHERE id = ?";
        List<User> users = executeQuery(sql, id);
        return users.isEmpty() ? null : users.get(0);
    }
    
    public List<User> findAll() {
        String sql = "SELECT * FROM users";
        return executeQuery(sql);
    }
    
    public int save(User user) {
        String sql = "INSERT INTO users(username, email) VALUES(?, ?)";
        return executeUpdate(sql, user.getUsername(), user.getEmail());
    }
}